﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace The_Last_Knights.GUI
{
    public abstract class Button: Element2D
    {
        #region Attributes
        protected Text _text;
        protected List<Texture2D> _state;        
        protected int _currentState;
#endregion

        #region Properties
        public Text Text
        {
            get { return _text; }
            set { _text = value; }
        }

        public List<Texture2D> State
        {
            get { return _state; }
            set { _state = value; }
        }

        public int CurrentState
        {
            get { return _currentState; }
            set { _currentState = value; }
        }
#endregion

        protected Button(Game game, Text text, string normal, string hover, string push, 
            string ID, Vector2 pos, Vector2 size, Texture2D background)
            : base(game, ID, pos, size, background)
        {
            _text = text;
            _state = new List<Texture2D>();

            Texture2D texture2D = null;
            try
            {
                texture2D = Game.Content.Load<Texture2D>(GameAssertPath.BUTTON_PATH + normal);
            }
            catch(Exception)
            {
                
            }
            finally
            {
                _state.Add(texture2D);
                texture2D = null;
            }
            try
            {
                texture2D = Game.Content.Load<Texture2D>(GameAssertPath.BUTTON_PATH + hover);
            }
            catch (Exception)
            {                                
            }
            finally
            {
                _state.Add(texture2D);
                texture2D = null;
            }

            try
            {
                texture2D = Game.Content.Load<Texture2D>(GameAssertPath.BUTTON_PATH + push);
            }
            catch (Exception)
            {                                
            }
            finally
            {
                _state.Add(texture2D);                
            }
                        
            _currentState = 0;
        }

        public virtual void ChangeHoverState()
        {
            if (_state[1] != null)            
                _currentState = 1;                            
        }

        public virtual void ChangePushState()
        {
            if (_state[2] != null)
                _currentState = 2;
        }

        public virtual void ChangeNormalState()
        {
            if (_state[0] != null)
                _currentState = 0;
        }

        public override void Update(GameTime gameTime)
        {
            MouseState ms = Mouse.GetState();
            Point mousePoint = new Point(ms.X, ms.Y);
            if (new Rectangle((int) _position.X, (int) _position.Y, (int) _size.X, (int) _size.Y).Contains(mousePoint))
            {
                ChangeHoverState();
                OnEvent(this, "EVENT_GUI_BUTTON_HOVER", null, null);
            }
            else
                ChangeNormalState();
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            SpriteBatch sb = GetSpriteBatch();
            if (sb != null)
            {
                sb.Begin();
                if (_state[_currentState] != null)
                    sb.Draw(_state[_currentState], new Rectangle((int) _position.X, (int) _position.Y, (int) _size.X, (int) _size.Y), Color.White);
                sb.End();                

                if (_text == null)
                    return;

                DrawText(sb);
            }            
        }

        protected virtual void DrawText(SpriteBatch sb)
        {
            Vector2 textLen = _text.GetBoundary();
            Vector2 pos = new Vector2(_position.X + (_size.X - textLen.X) / 2, _position.Y + (_size.Y - textLen.Y) / 2);
            Color color = new Color(220, 220, 220, 255);
            if (_currentState == 0)
                _text.Draw(sb, pos, color);
            if (_currentState == 1)
                _text.Draw(sb, pos, Color.White);
            if (_currentState == 2)
                _text.Draw(sb, pos + new Vector2(0, 1), Color.White);
        }
    }
}
